﻿package visualizer.code
{
	import flash.geom.ColorTransform;
	import flash.events.Event;
	import flash.display.MovieClip;
	
	public class TerekVisualizer extends Visualizer
	{
		// Variables
		var roseContainer:MovieClip;
		var stageLightContainer:MovieClip;
		var drummerArmContainer:MovieClip;
		var theStage:TerekStage;
		var man:StickMan;
		var rose:Rose;
		var light:StageLight;
		var arm:DrummerArm;
		
		
		public function TerekVisualizer()
		{
			// Setup variables
			theStage = new TerekStage();
			theStage.x = 300;
			theStage.y = 200;
			addChild(theStage);
			man = new StickMan();
			man.x = 300;
			man.y = 200;
			
			// Setup the container that will hold instances of the Rose and StageLight symbols
			roseContainer = new MovieClip();
			addChild(roseContainer);
			stageLightContainer = new MovieClip();
			addChild(stageLightContainer);
			drummerArmContainer = new MovieClip();
			addChild(drummerArmContainer);
			addChild(man);
			
			// Setup Listeners
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			addEventListener(BeatEvent.LOW_BAND_BEAT, handleLowBeat);
			addEventListener(BeatEvent.MID_BAND_BEAT, handleMidBeat);
			addEventListener(BeatEvent.HIGH_BAND_BEAT, handleHighBeat);
		}
		
		override protected function drawFrame()
		{
			//super.drawFrame();
		}
		
		private function onEnterFrame(e:Event)
		{
			gotoAndStopByVol(man);
			
			
			// Delete Rose symbols when the finish playing
			for (var i:uint = 0; i < roseContainer.numChildren; i++)
			{
				var child:Rose = roseContainer.getChildAt(i) as Rose;
				
				// Only remove the child if it has finished playing
				if (child.currentFrame == child.totalFrames)
				{
					roseContainer.removeChild(child);
				}
			}
			
			
			
			// Delete StageLight symbols when the finish playing
			for (i = 0; i < stageLightContainer.numChildren; i++)
			{
				var child2:StageLight = stageLightContainer.getChildAt(i) as StageLight;
				
				// Only remove the child if it has finished playing
				if (child2.currentFrame == child2.totalFrames)
				{
					stageLightContainer.removeChild(child2);
				}
			}
			
			
			
			// Delete DrummerArm symbols when the finish playing
			for (i = 0; i < drummerArmContainer.numChildren; i++)
			{
				var child3:DrummerArm = drummerArmContainer.getChildAt(i) as DrummerArm;
				
				// Only remove the child if it has finished playing
				if (child3.currentFrame == child3.totalFrames)
				{
					drummerArmContainer.removeChild(child3);
				}
			}
			
			
			
			
			// Move the box
			/* b.x = avgVol()*1200;
			b.y = avgVol()*800;
			b.rotation +=5; */
		}
		
		private function handleLowBeat(event:BeatEvent)
		{
			
			var arm:DrummerArm = new DrummerArm();
			arm.x = 400;
			arm.y = 165;
			drummerArmContainer.addChild(arm);
			
		}
		
		private function handleMidBeat(event:BeatEvent)
		{
			
			var light:StageLight = new StageLight();
			light.x = 215;
			light.y = 322;
			stageLightContainer.addChild(light);
			
		}
		
		private function handleHighBeat(event:BeatEvent)
		{
			
			var rose:Rose = new Rose();
			rose.x = random(150, 450);
			rose.y = 300;
			rose.rotation = random(0,90);
			roseContainer.addChild(rose);
			
		}
		
		//---------- Convenience Functions ----------//		
		private function random(low:Number=0, high:Number=1):Number
		{
			return Math.floor(Math.random() * (1+high-low)) + low;
		}		
		
		private function gotoAndStopByVol(anim:MovieClip)
		{
			var newFrame:uint = Math.floor((avgVol()*anim.totalFrames)/.9);
			if(newFrame > anim.totalFrames) newFrame = anim.totalFrames;
			anim.gotoAndStop(newFrame);
		}
	}
}